package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlog;
import pers.myz.service.SnsBlogService;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-blog")
public class SnsBlogController {

    @Autowired
    private SnsBlogService snsBlogService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsBlog blog){
        return snsBlogService.save(blog)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody SnsBlog blog){
        return snsBlogService.updateById(blog)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return snsBlogService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String typeId = (String)param.get("typeId");
        String title = (String)param.get("title");
        String creatorId = (String)param.get("creatorId");

        Page<SnsBlog> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<SnsBlog> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(SnsBlog::getBlogStatus);
        lambdaQueryWrapper.orderByDesc(SnsBlog::getCreateTime);
        if(StringUtils.isNotBlank(typeId) && !"null".equals(typeId)) {
            lambdaQueryWrapper.like(SnsBlog::getTypeId, typeId);
        }
        if(StringUtils.isNotBlank(title) && !"null".equals(title)){
            lambdaQueryWrapper.like(SnsBlog::getTitle,title);
        }
        if(StringUtils.isNotBlank(creatorId) && !"null".equals(creatorId)){
            lambdaQueryWrapper.eq(SnsBlog::getCreatorId,creatorId);
        }
        IPage result = snsBlogService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
